﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Aliphant.UI;
using Aliphant.UI.Widgets;

namespace TS.Register
{
    /// <summary>
    /// RegisterView.xaml 的交互逻辑
    /// </summary>
    public partial class RegisterView : BaseWidget, IWidget
    {
        public RegisterView()
        {
            InitializeComponent();
        }

        public void Init(Widget widget)
        {

        }

        public void QuickAction1()
        {

        }

        public void QuickAction2()
        {

        }

        public void Close()
        {

        }

        public void SetScriptPath(string scriptPath)
        {

        }



        public string? QuickButton1Tip => string.Empty;

        public string? QuickButton2Tip => string.Empty;

        public string? QucikButton1Icon => string.Empty;

        public string? QucikButton2Icon => string.Empty;

        private void Encrypt(object sender, RoutedEventArgs e)
        {
            string dates = (string)date.SelectedItem;
            int days = GetDays(dates);

            string plaintext = input.Text + DateTime.Now.AddDays(days).ToString("yyyy-MM-dd HH:mm:ss");
            string key = "9Mq9c1/mr2xWQpPk";

            string ciphertext = AesEncrypt(plaintext, key, false);
            output.Text = ciphertext;
        }

        private void Decrypt(object sender, RoutedEventArgs e)
        {
            string plaintext = input.Text;
            string key = "9Mq9c1/mr2xWQpPk";

            string ciphertext = AesDecrypt(plaintext, key, false);
            output.Text = ciphertext;
        }

        public static string AesEncrypt(string content, string key, bool autoHandle = true)
        {
            byte[] keyArray = Encoding.UTF8.GetBytes(key);
            if (autoHandle)
            {
                keyArray = GetAesKey(keyArray, key);
            }
            byte[] toEncryptArray = Encoding.UTF8.GetBytes(content);
            SymmetricAlgorithm des = Aes.Create();
            des.Key = keyArray;
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = des.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray);
        }

        public static string AesDecrypt(string content, string key, bool autoHandle = true)
        {
            try
            {
                byte[] keyArray = Encoding.UTF8.GetBytes(key);
                if (autoHandle)
                {
                    keyArray = GetAesKey(keyArray, key);
                }
                byte[] toEncryptArray = Convert.FromBase64String(content);
                SymmetricAlgorithm des = Aes.Create();
                des.Key = keyArray;
                des.Mode = CipherMode.ECB;
                des.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = des.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return Encoding.UTF8.GetString(resultArray);
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        private static byte[] GetAesKey(byte[] keyArray, string key)
        {
            byte[] newArray = new byte[16];
            if (keyArray.Length < 16)
            {
                for (int i = 0; i < newArray.Length; i++)
                {
                    if (i >= keyArray.Length)
                    {
                        newArray[i] = 0;
                    }
                    else
                    {
                        newArray[i] = keyArray[i];
                    }
                }
            }
            return newArray;
        }

        private int GetDays(string dates)
        {
            switch (dates)
            {
                case "30天":
                    return 30;
                case "半年":
                    return 180;
                case "1年":
                    return 365;
                case "3年":
                    return 365 * 3;
                case "5年":
                    return 365 * 5;
                case "10年":
                    return 3650;
                default:
                    return 3;
            }
        }
    }
}
